What is steno?
The steno npm package is a simple, fast, and lightweight library for writing JSON files atomically. It ensures that data is written to disk safely and without corruption, making it ideal for applications that need to frequently update JSON files.
What are steno's main functionalities?
Atomic Write
This feature allows you to write JSON data to a file atomically. The `writeFile` method ensures that the data is written safely, preventing file corruption.
const steno = require('steno');
const fs = require('fs');
const data = { key: 'value' };
steno.writeFile('data.json', JSON.stringify(data), (err) => {
if (err) throw err;
console.log('Data written successfully');
});
Synchronous Write
This feature allows you to write JSON data to a file synchronously. The `writeFileSync` method ensures that the data is written safely and the operation is completed before moving on to the next line of code.
const steno = require('steno');
const fs = require('fs');
const data = { key: 'value' };
try {
steno.writeFileSync('data.json', JSON.stringify(data));
console.log('Data written successfully');
} catch (err) {
console.error('Error writing data:', err);
}
Other packages similar to steno
write-file-atomic
The write-file-atomic package provides similar functionality to steno by ensuring that files are written atomically. It is designed to handle the same use cases, such as preventing file corruption during write operations. However, write-file-atomic offers more configuration options and is more widely used in the community.
fs-extra
The fs-extra package extends the native Node.js fs module with additional methods, including atomic write operations. While it offers a broader range of file system utilities compared to steno, it is also heavier and includes more features than just atomic writes.
Steno
Specialized fast async file writer
Steno makes writing to the same file often/concurrently fast and safe.
Used in lowdb.
https://en.wikipedia.org/wiki/Stenotype
Features
- ⚡ Fast (see benchmark)
- 🐦 Lightweight (~6kb)
- 👍 ⚛️ Safe: No partial writes (writes are atomic)
- 👍 🏁 Safe: No race conditions (writes are ordered even if they're async)
Usage
import { Writer } from 'steno'
const file = new Writer('file.txt')
async function save() {
await file.write('some data')
}
Benchmark
npm run benchmark
(see src/benchmark.ts
)
Write 1KB data to the same file x 1000
fs : 62ms
steno : 1ms
Write 1MB data to the same file x 1000
fs : 2300ms
steno : 5ms
Steno uses a smart queue and avoids unnecessary writes.
License
MIT - Typicode